package com.uyaer.magic
{
	import com.uyaer.magic.common.Common;
	import com.uyaer.magic.layer.BackgroundLayer;
	import com.uyaer.magic.layer.CanvasLayer;
	import com.uyaer.magic.layer.MapLayer;
	import com.uyaer.magic.layer.MenuLayer;
	import com.uyaer.magic.levels.LevelData;
	import com.uyaer.magic.levels.LevelManager;
	
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import nape.geom.Vec2;
	import nape.phys.Body;
	import nape.phys.BodyType;
	import nape.phys.Material;
	import nape.shape.Circle;
	import nape.shape.Polygon;
	import nape.shape.Shape;
	import nape.space.Space;
	import nape.util.ShapeDebug;
	
	public class Game extends Sprite
	{
		private var background:BackgroundLayer;
		private var map:MapLayer;
		private var canvas:CanvasLayer;
		private var menu:MenuLayer;
		
		private var space:Space;
		private var debug:ShapeDebug;
		private var ball:Body;
		public function Game()
		{
			if(stage){
				onAdded();
			}else{
				this.addEventListener(Event.ADDED_TO_STAGE,onAdded);
			}
		}
		
		private function onAdded(e:Event = null):void{
			background = new BackgroundLayer();
			map = new MapLayer();
			menu = new MenuLayer();
			canvas = new CanvasLayer();
			addChild(background);
			addChild(map);
			addChild(menu);
			addChild(canvas);
			
			
			space = new Space(new Vec2(0,600));
			Common.space = space;
			debug = new ShapeDebug(800,480);
			addChild(debug.display);
			stage.addEventListener(Event.ENTER_FRAME,loop);
			
			LevelManager.i.select(1,1);
		}
		
		private function loop(e:Event):void{
			space.step(1/stage.frameRate);
			debug.clear();
			debug.draw(space);
			debug.flush();
		}
	}
}